package com.hbm.usercenter.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseActivityCommon;
import com.hbm.usercenter.fragment.UpdateInfoFragment;
import com.hbm.usercenter.fragment.UpdatePwdFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateActivity extends BaseActivityCommon {
    @Bind(R.id.tv_info)
    TextView mTvInfo;
    @Bind(R.id.tv_pwd)
    TextView mTvPwd;
    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.fl_content)
    FrameLayout mFlContent;

    private UpdateInfoFragment mUpdateInfoFragment;
    private UpdatePwdFragment mUpdatePwdFragment;
    private FragmentTransaction mTransaction;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_update);
        ButterKnife.bind(this);
        initToolbar("个人资料");


        setSelect(0);
//        LoadingPager loadingPager = mUpdateInfoFragment.getLoadingPager();
//        if (loadingPager == null) {
//            LogUtils.s("啊啊啊是空-----------");
//        } else {
//            loadingPager.triggerLoadData();
//        }
        mTvInfo.setBackgroundColor(0xFFF1F1F1);
    }

    @OnClick({R.id.tv_info, R.id.tv_pwd})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_info:
                mTvInfo.setBackgroundColor(0xFFF1F1F1);
                mTvPwd.setBackgroundColor(0xFFFAFAFA);
//                LoadingPager loadingPager = mUpdateInfoFragment.getLoadingPager();
//                if (loadingPager == null) {
//                    LogUtils.s("啊啊啊是空-----------");
//                } else {
//                    loadingPager.triggerLoadData();
//                }
                setSelect(0);

                break;
            case R.id.tv_pwd:
                mTvPwd.setBackgroundColor(0xFFF1F1F1);
                mTvInfo.setBackgroundColor(0xFFFAFAFA);

                setSelect(1);
//                LoadingPager loadingPager1 = mUpdatePwdFragment.getLoadingPager();
//                if (loadingPager1 == null) {
//                    LogUtils.s("啊啊啊是空-----------");
//                } else {
//                    loadingPager1.triggerLoadData();
//                }
                break;
        }
    }

    public void setSelect(int select) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        mTransaction = mFragmentManager.beginTransaction();
        hideFragment();
        switch (select) {
            case 0:
                if (mUpdateInfoFragment == null) {
                    mUpdateInfoFragment = new UpdateInfoFragment();

                    mTransaction.add(R.id.fl_content, mUpdateInfoFragment);
                }
                mTransaction.show(mUpdateInfoFragment);
                break;

            case 1:
                if (mUpdatePwdFragment == null) {
                    mUpdatePwdFragment = new UpdatePwdFragment();
                    mTransaction.add(R.id.fl_content, mUpdatePwdFragment);
                }
                mTransaction.show(mUpdatePwdFragment);
                break;
        }
        mTransaction.commit();
    }

    private void hideFragment() {
        if (mUpdateInfoFragment != null) {
            mTransaction.hide(mUpdateInfoFragment);
        }
        if (mUpdatePwdFragment != null) {
            mTransaction.hide(mUpdatePwdFragment);
        }
    }


}
